library(dplyr)
library(readxl)
library(ggplot2)
library(tidyverse)
library(readr)
library(scales)
library(estimatr)
library(cregg)

####CONJOINT FIGURE####


dac<- read_csv('/Users/celinascott-buechler/DFP/dac_survey/Replication/Analysis/National Survey/Data/conjoint_long.csv', col_names = TRUE)

dac$funding[dac$Funding == "Private funds" | dac$Funding=="Closing tax loophole for polluting industries" | dac$Funding=="Tax on polluting industries"] <- "industry-funded"
dac$funding[dac$Funding == "Income tax on the wealthiest Americans"| dac$Funding == "Economy-wide tax"] <- "not industry-funded"

dac$owner[dac$Owner== "Fossil fuel company" | dac$Owner== "Direct air capture company" | dac$Owner== "Cooperatively owned by community members and community organizations"] <- "non-government owner"
dac$owner[dac$Owner== "State or local government"  | dac$Owner== "National government"] <- "government owner"

dac$energy[dac$Energy== "New or expanded wind/solar energy" | dac$Energy== "New or expanded geothermal energy (heat from deep in the earth)"| dac$Energy=="New or expanded nuclear energy"] <- "non-fossil energy"
dac$energy[dac$Energy=="New or expanded fossil fuel energy"|dac$Energy=="Existing electricity grid energy"] <- "fossil energy"

dac$community[dac$Community == "No consultation"| dac$Community == "Written comments accepted"] <- "low involvement"
dac$community[dac$Community == "Workshops held to collect feedback"| dac$Community =="An elected community advisory board has voting power over project decisions" | dac$Community == "Community members have direct voting power over project decisions"] <- "high involvement"

dac$storage[dac$Storage == "Transported out of community by pipeline to be stored elsewhere"| dac$Storage =="Transported out of community by trucks or rail to be stored elsewhere"] <- "stored elsewhere"
dac$storage[dac$Storage == "Stored underground on-site using mineralization (injecting underground to turn carbon dioxide into rock)"| dac$Storage == "Stored underground on-site in depleted oil and gas wells (injecting into empty wells)"| dac$Storage == "Used for enhanced oil recovery on-site (extracting oil while potentially storing some carbon dioxide)" ] <- "stored on-site"

dac$benefits[dac$Benefits == "None"| dac$Benefits == "1%"|dac$Benefits =="5%"] <- "low benefits"

dac$benefits[dac$Benefits == "10%"| dac$Benefits =="20%"] <- "high benefits"

dac$jobs[ dac$Jobs =="Long-term local jobs guaranteed without unionization commitment" | dac$Jobs =="Long-term local jobs guaranteed with unionization commitment" ] <- "long-term jobs"
dac$jobs[dac$Jobs == "No local jobs guaranteed"| dac$Jobs == "Short-term local jobs guaranteed for construction with union requirement" |dac$Jobs == "Short-term local jobs guaranteed for construction without union requirement"] <- "short-term or no jobs"

dac$funding <- factor(dac$funding)
dac$owner <- factor(dac$owner)
dac$energy <- factor(dac$energy)
dac$community <- factor(dac$community)
dac$storage <- factor(dac$storage)
dac$benefits <- factor(dac$benefits)
dac$jobs <- factor(dac$jobs)

dac<-dac%>%
  dplyr::mutate(funding=relevel(funding,ref="industry-funded"),
                owner=relevel(owner,ref="non-government owner"),
                energy=relevel(energy,ref="fossil energy"),
                community=relevel(community, ref="low involvement"),
                storage=relevel(storage, ref= "stored on-site"),
                benefits=relevel(benefits, ref="low benefits"),
                jobs=relevel(jobs, ref="short-term or no jobs"))

par(mar=c(5,6,4,1)+.1)
plot(amce(dac, Choice~funding+owner+energy+community+storage+benefits+jobs, weights=dac$nationalweight,id = ~ResponseId), size = 2)+ ggplot2::theme_bw()

df<- data.frame(amce(dac_dac, Choice~funding+owner+energy+community+storage+benefits+jobs, weights=dac_dac$nationalweight,id = ~ResponseId))

View(df)
